﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace teacherplanner
{
    public partial class PlannerForm : Form
    {
        OleDbConnection con;
        DataSet ds1;
        DataSet ds2;
        OleDbDataAdapter da1;
        OleDbDataAdapter da2;
        int MaxRows = 0;
        int inc = 0;
        DataRow lessonRow;
        bool loaded = false;


        public PlannerForm()
        {
            InitializeComponent();
        }

        private int findToday()
        {
            int CurRow = 0;
            DateTime nowTime = DateTime.Now;
            for (int i = 0; i < MaxRows; i++)
            {
                DataRow dRow = ds1.Tables["TeachingDays"].Rows[i];
                DateTime CurRecordDate = (DateTime)dRow.ItemArray.GetValue(1);
                if (CurRecordDate < nowTime)
                {
                    CurRow = i;
                }
                else
                {
                    CurRow = i;
                    break;
                }
            }
            return CurRow;

        }



        private void NavigateRecords()
        {
            DataRow dRow = ds1.Tables["TeachingDays"].Rows[inc];
            DateTime CurRecordDate = (DateTime)dRow.ItemArray.GetValue(1);
            int myDayNum = (int)dRow.ItemArray.GetValue(2);
            int myDayRecord = (int)dRow.ItemArray.GetValue(0);
            String myDateString = Functions.myFunctions.DayString(myDayNum) + " " + CurRecordDate.ToLongDateString();
            label1.Text = myDateString;

            con = new System.Data.OleDb.OleDbConnection();
            ds2 = new DataSet();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + teacherplanner.Properties.Settings.Default.CurrentPlanner;
            string sql2 = "SELECT * FROM LessonDetails WHERE DayID = " + myDayRecord;
            da2 = new OleDbDataAdapter(sql2, con);
            con.Open();
            da2.Fill(ds2, "LessonDetails");
            int MaxRows2 = ds2.Tables["LessonDetails"].Rows.Count;
            if (MaxRows2 < 8)
            {
                form6.Text = "";
                form7.Text = "";
                form8.Text = "";
            }
            for (int i = 1; i <= MaxRows2; i++)
            {
                lessonRow = ds2.Tables["LessonDetails"].Rows[i - 1];
                string ClassName = lessonRow.ItemArray.GetValue(3).ToString();
                int LessID = (int)lessonRow.ItemArray.GetValue(0);
                string curControl = "Form" + i;
                object[] findform = this.Controls.Find(curControl,true);
                Label mylabel = (Label)findform[0];
                mylabel.Text = ClassName;
                mylabel.Tag = LessID;
            }
        }

        private void PlannerForm_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + teacherplanner.Properties.Settings.Default.CurrentPlanner;
            string sql = "SELECT * FROM TeachingDays";
            da1 = new OleDbDataAdapter(sql, con);

            con.Open();

            da1.Fill(ds1, "TeachingDays");
            MaxRows = ds1.Tables["TeachingDays"].Rows.Count;
            inc = findToday();
            NavigateRecords();
            con.Close();
            con.Dispose();
            loaded = true;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }

            else
            {
                MessageBox.Show("No More Rows");
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("First Record");
            }

        }

        private void PlannerForm_Resize(object sender, EventArgs e)
        {
            int wide = this.Width;
            this.label1.SetBounds((int)((wide / 2) - 200), 25, 400, 25);
        }

        private void formButton_Click(object sender, EventArgs e)
        {
            Label myButton = (Label)sender;
            teacherplanner.EditClassDetails myForm = new teacherplanner.EditClassDetails((int)myButton.Tag,myButton.Text);
            myForm.Show();
        }

        private void PlannerForm_Activated(object sender, EventArgs e)
        {
            if (loaded == true)
            {
                NavigateRecords();
            }
        }
    }
}
